const app = getApp();
import util from "../../../../utils/util";
var page = 1;
var size = 15;
var hasContent = true;
Page({
    data: {
        citycode: "",
        info: {},
    },
    onLoad: function(options) {
        this.setData({
            citycode: wx.getStorageSync("citycode"),
        });
        this.user = decodeURIComponent(options.user);
        this.ids = [];
        util.getUrl(app.globalData.url).then(res => {
            util.request("share.php", {
                action: "user_decode",
                user: decodeURIComponent(this.user),
                size: 10,
                page
            }, "POST").then(res => {
                if (res.data.message == "success" && res.data.data.count) {
                    res.data.data.data.forEach(item => {
                        this.ids.push(item.id);
                        if (item.value && item.value != "undefined")
                            item.value = JSON.parse(item.value);
                    });
                    this.setData({
                        info: res.data.data
                    })
                }
            });

            if (!wx.getStorageSync("user_id")) {
                util.request("user.php", {
                    action: "get_userid",
                    baidu_openid: res.data.data.openid,
                }, "POST").then(res => {
                    wx.setStorageSync("user_id", res.data.data.id)
                });
            }
        });

        // 监听页面加载的生命周期函数
    },

    share() {
        wx.showModal({
            title: '温馨提示',
            content: '是否将此收藏夹内容给添加到您的收藏夹',
            showCancel: true,
            cancelText: '取消',
            cancelColor: '#000000',
            confirmText: '确定',
            confirmColor: '#3CC51F',
            success: (result) => {
                if (result.confirm) {
                    util.request("share.php", {
                        action: "user_decode",
                        openid: wx.getStorageSync("openid"),
                        user_id: wx.getStorageSync("user_id"),
                        ids: this.ids.join(","),
                    }, "POST").then(res => {
                        if (res.data.message == "success") {
                            wx.showToast({
                                title: '添加收藏成功',
                                icon: 'none',
                                image: '',
                                duration: 1500,
                                mask: false,
                                success: (result) => {
                                    wx.reLaunch({
                                        url: '/pages/mine/collect/index/index',
                                    });
                                },
                            });
                        }
                    });
                }
            },
        });
    },



    onReachBottom() {
        ++page;
        if (hasContent)
            util.request("share.php", {
                action: "user_decode",
                user: this.user,
                size,
                page
            }, "POST").then(res => {
                if (res.data.data.count) {
                    res.data.data.data.forEach(item => this.data.info.data.push(item));
                    this.setData({ info: this.data.info });
                } else {
                    wx.showToast({
                        title: '没有更多数据了',
                        icon: 'none',
                        image: '',
                        duration: 1500,
                    });
                    hasContent = false;
                }
            });
    },
});